/*
 * Created by JFormDesigner on Wed Jun 07 09:02:03 CST 2023
 */

package gui.admin;

import dao.ProductCategoryDao;
import dao.UserDao;
import entity.ProductCategory;
import entity.User;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 * @author Liubuzhu
 */
public class AddProductCategoryFrame extends JFrame {
    public AddProductCategoryFrame() {
        initComponents();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        label2 = new JLabel();
        productCategoryId = new JTextField();
        productCategoryName = new JTextField();
        addBtn = new JButton();
        cancelBtn = new JButton();

        //======== this ========
        setTitle("\u6dfb\u52a0\u5546\u54c1\u5206\u7c7b\u9875\u9762");
        setPreferredSize(new Dimension(500, 400));
        setVisible(true);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u5546\u54c1\u5206\u7c7b\u7f16\u53f7");
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label1);
        label1.setBounds(75, 45, 100, 40);

        //---- label2 ----
        label2.setText("\u5546\u54c1\u5206\u7c7b\u540d\u79f0");
        label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label2);
        label2.setBounds(75, 160, 100, 40);
        contentPane.add(productCategoryId);
        productCategoryId.setBounds(190, 45, 205, 45);
        contentPane.add(productCategoryName);
        productCategoryName.setBounds(190, 160, 205, 45);

        //---- addBtn ----
        addBtn.setText("\u6dfb\u52a0");
        addBtn.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(addBtn);
        addBtn.setBounds(205, 265, 95, 50);

        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String productCategoryIdTemp = productCategoryId.getText();//获取商品类别编号输入框的内容
                String productCategoryNameTemp = productCategoryName.getText();//获取类名
                //根据获取的数据构造一个商品类别对象
                ProductCategory productCategory = new ProductCategory(productCategoryIdTemp, productCategoryNameTemp);

                ProductCategoryDao productCategoryDao = new ProductCategoryDao();

                boolean result = false;
                try {
                    //执行添加方法
                    result = productCategoryDao.addProductCategory(productCategory);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                if (result) {
                    //如果返回结果为真
                    JOptionPane.showMessageDialog(null, "添加成功");
                } else {
                    //如果返回结果为假
                    JOptionPane.showMessageDialog(null, "添加失败");
                }
            }
        });

        //---- cancelBtn ----
        cancelBtn.setText("\u53d6\u6d88");
        cancelBtn.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(cancelBtn);
        cancelBtn.setBounds(345, 265, 95, 50);
        //给取消按钮填加交互事件
        cancelBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                dispose();//关闭当前窗口
                try {
                    AdminMainFrame adminMainFrame = new AdminMainFrame();//回到管理员主界面
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JLabel label2;
    private JTextField productCategoryId;
    private JTextField productCategoryName;
    private JButton addBtn;
    private JButton cancelBtn;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
    public static void main(String[] args) {
        AddProductCategoryFrame addProductCategoryFrame = new AddProductCategoryFrame();
    }
}
